using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Threading;

using SqlWebAdmin.CSharp;

public partial class CreateDatabase : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
           
                lblError.Text = string.Empty;
                lblError.Visible = false;


                Server smoServer = SmoHelper.GetServer();
                try
                {
                    int i = 1;
					string databaseName = Resources.LocalizedText.CreateDatabaseDefaultDatabaseName + i.ToString(Thread.CurrentThread.CurrentUICulture);

                    while (smoServer.Databases.Contains(databaseName))
                    {
                        i++;
						databaseName = Resources.LocalizedText.CreateDatabaseDefaultDatabaseName + i.ToString(Thread.CurrentThread.CurrentUICulture);
                    }
                    txtDatabaseName.Text = databaseName;
                }
                catch (SmoException smoEx)
                {
                    lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(smoEx));
                    lblError.Visible = true;
                }
                catch (System.Exception ex)
                {
                    lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(ex));
                    lblError.Visible = true;
                }
				finally
				{
					smoServer.ConnectionContext.Disconnect();
				}
            }

        
    }

    protected void cmdCreate_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
           
                lblError.Visible = false;

                Server smoServer = SmoHelper.GetServer();
                try
                {
                    if (smoServer.Databases.Contains(txtDatabaseName.Text))
                    {
                        lblError.Visible = true;
                        lblError.Text = "A database with this name already exists.";
                    }
                    else
                    {
                        Database database = new Database(smoServer, txtDatabaseName.Text);
                        database.Create();

                        Response.Redirect("Tables.aspx?database=" + HttpUtility.UrlEncode(txtDatabaseName.Text), false);
                    }
                }
                catch (SmoException smoEx)
                {
                    lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(smoEx));
                    lblError.Visible = true;
                }
                catch (System.Exception ex)
                {
                    lblError.Text = HttpUtility.HtmlEncode(Common.ConcatenateExceptions(ex));
                    lblError.Visible = true;
                }
            }

      
    }
}
